<?php

/** 
 * 快递员网点公司关系表模型
 * @author cheng
 */

namespace Home\Model;
use Think\Model;
class ExpressBranchComDzModel extends Model {
    //快递员信息表
    protected $tableName = 'tp_express_branch_com_dz'; //定义表名
    
    /**
     * 根据快递员id获取和快递员合作的站点id
     */
    public function getCompanyId($courierId) {
        $data = $this->where(['f_exp_id' => $courierId, 'f_branch_state' => 0])->getField('f_station_id', true);
        return $data;
    }
    
    /**
     * @desc 站点合作、取消合作
     * @author quyang
     * @update 2016-11-26
     */
    public function changeBranchState($method,$args,$stationId){
        $where = array('f_exp_id'=>$method,'f_station_id'=>$stationId);
        $data = $this->where($where)->save(array('f_branch_state'=>$args));
        return $data;
    }


    /***
     * Function continual_use
     * @param $stationId
     * @param $expId
     * @param $status
     * @return bool
     *
     * Author: 黄盖
     * Date: 2017-3-15 13:51
     * Info: 站点禁止/允许快递员入库
     */
    public function continual_use( $stationId, $expId, $status )
    {
        return $this ->where("`f_exp_id` = '{$expId}' AND `f_station_id` = '{$stationId}'") ->save( Array( 'continual_use' => $status ) );
    }

    /**
     * @desc 获取合作快递员接口
     * @author quyang
     * @update 2016-11-28
     */
    public function queryCourier($method,$page,$pagenum){
        $where = array('eb.f_station_id'=>$method);
        $count = $this->alias('eb')->where($where)->count();
        $data['list'] = $this->alias('eb')
            ->join('tp_user u on eb.f_exp_id=u.f_user_id','LEFT')
            ->where($where)
            ->field('
                     eb.f_exp_id as fExpId,
                     eb.f_com_name as fComName,
                     eb.f_com_id as fComId,
                     eb.f_branch_state as fBranchState,
                     eb.f_exp_name as fExpName,
                     u.f_user_name as fUserName
                     ')
            ->page($page,$pagenum)
            ->select();
        $data['totalPage'] = ceil($count/$pagenum);  //总页数
        $data['totalCount'] = $count; //总条数
        $data['packetInfoList'] = $page; //当前页
        return $data;
    }


    /**
     * Function getCooperationCompany
     *
     * Author: quyang
     * Date: 2017-2-7 10:45
     * Info: 站点已合作，申请中快递
     */
    public function getCooperationCompany($method){

        $where['e.f_station_id'] = $method['stationId'];
        $where['e.f_branch_state'] = $method['status'];

        $page = $method['page'] ? $method['page'] : 1;

        $data['page'] = "$page";
        $data['pageNum'] = '20';
        $data['pagetotle'] = $this->alias('e')->where($where)->join('tp_user u on e.f_exp_id=u.f_user_id','LEFT')->count('f_exp_id');
        $data['list'] = $this->alias('e')
            ->where($where)
            ->join('tp_user u on e.f_exp_id=u.f_user_id','LEFT')
            ->field("
                e.f_exp_name as fExpName,
                e.f_com_name as fComName,
                e.f_branch_state as fBranchState,
                FROM_UNIXTIME(`e`.`f_create_time`,'%Y-%m-%d') as fCreateTime,
                u.f_user_name as fUserName,
                u.f_user_id as fUserId,
                e.continual_use as continualUse
            ")
            ->page($page,20)
            ->select();

        return $data;

    }

    /**
     * @Desc  : 验证相同的快递是否已经合作
     * @Author：quyang
     * @Param ：
     * @Time  ：2017/3/20 13:55
     * @Update：2017/3/20
     **/
    public function cooperationExpress( $stationId , $comId )
    {

        $where['f_station_id'] = $stationId;
        $where['f_com_id'] = $comId;
        $where['f_branch_state'] = '0';

        return $this->where( $where )->find();

    }
    
}